<?php

/**
 * 自动生成 MerOrderService
 * 
 * Apps/Common/Service/MerOrderService.class.php
 * ryadmin @ 2015-07-02
 */
namespace Common\Service;

class MerOrderService {
  private static $instance;
  public static function instance() {
    if (self::$instance == null) {
      $instance = new MerOrderService ();
    }
    
    return $instance;
  }
  function get_default_row() {
    return array (
        'id' => '',
        'mer_id' => '0',
        'order_sn' => '',
        'user' => '',
        'area' => '',
        'address' => '',
        'postal_code' => '',
        'create_date' => date ( 'Y-m-d H:i:s' ),
        'pay_date' => '',
        'state' => '',
        'order_amount' => '',
        'pay_sn' => '',
        'shipping_type' => '',
        'shipping_code' => '',
        'shipping_fee' => '',
        'shop_goods' => '',
        'message' => '',
        'phone' => '',
        'name' => '' 
    );
  }
  function get_by_id($id) {
    $MerOrder = M ( 'MerOrder' );
    $data = $MerOrder->find ( $id );
    return $data ? $data : array ();
  }
  function get_by_cond($config) {
    $default = array (
        'page' => 1,
        'page_size' => 6,
        'status' => '',
        'count' => FALSE,
        'order' => 'DESC',
        'sort' => 'id' 
    );
    
    $config = extend ( $config, $default );
    
    $MerOrder = M ( 'MerOrder' );
    
    $where = array ();
    $where ['status'] = empty ( $config ['status'] ) ? '0' : $config ['status'];
    if ($config ['count']) {
      return $MerOrder->where ( $where )
        ->count ();
    } else {
      $order = $config ['sort'] . ' ' . $config ['order'];
      $limit = ($config ['page'] - 1) * $config ['page_size'] . ' , ' . $config ['page_size'];
      $data = $MerOrder->where ( $where )
        ->limit ( $limit )
        ->order ( $order )
        ->select ();
      // echo $MerOrder->_sql();
      return $data ? $data : array ();
    }
  }
  function create($data, $is_ajax = true) {
    $MerOrder = M ( 'MerOrder' );
    $ret = $MerOrder->add ( $data );
    if ($is_ajax) {
      if ($ret) {
        return ajax_arr ( '添加成功', TRUE, array (
            'id' => $ret 
        ) );
      } else {
        return ajax_arr ( '添加失败', FALSE );
      }
    } else {
      if ($ret) {
        return $ret;
      } else {
        return 0;
      }
    }
  }
  function update($id, $data) {
    $MerOrder = M ( 'MerOrder' );
    
    $ret = $MerOrder->where ( "id = %d", $id )
      ->save ( $data );
    if ($ret) {
      return ajax_arr ( '编辑成功', TRUE );
    } else {
      return ajax_arr ( '编辑失败', FALSE );
    }
  }
  function delete($ids) {
    $MerOrder = M ( 'MerOrder' );
    $ret = $MerOrder->delete ( $ids );
    if ($ret == 0) {
      return ajax_arr ( '未删除任何数据', FALSE );
    } else if (! $ret) {
      return ajax_arr ( '删除失败', FALSE );
    } else {
      return ajax_arr ( '删除' . $ret . '行数据', TRUE );
    }
  }
}
